<?php
/**
 * 《社保信息》实现类
 *
 * @author 孙晓晔
 * @version $Id: Work.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/../finance/FinanceUtil.class.php');

class Social extends FinanceUtil {
	public $tbl = Constant::tbl_cw_social;
	
	public $data = array(
		'-' => '请选择',
		'1' => '深圳社保',
		'2' => '东莞社保',

		'9' => '其它',
	);
	
	public $status = array(
		'-' => '请选择',
		'1' => '等待审核',
		'2' => '正在处理',
		'3' => '处理完成',

		'9' => '其它',
	);
	
	public $orderby = array(
		'-' => '请选择',
		'emp' => '员工',

		'dept, id' => '部门',
		'a2' => '养老',
		'b2' => '医疗',
		'c2' => '工伤',
		'd2' => '失业',
		'e2' => '生育医疗',

		'status' => '状态',
		'1' => '创建时间',
		'2' => '更新时间',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}

	function pretreat($controller) {
		parent::pretreat($controller);
		
		$controller->assign_by_ref('data_option', $this->data);
	}

	function _name_option() {
		$t1 = strtotime("-13 month");
		$t2 = time();
	
		$arr = array('-'=>'请选择');
		while ($t1 < $t2) {
			$str = date("Y-m", $t2);
			$t2 = strtotime("-1 month", $t2);
	
			$arr[$str] = $str;
		}
	
		return $arr;
	}
	
	function find($controller, $request) {
		$this->pretreat($controller);

		// $controller->assign('t1', strtotime('-7 day'));
		
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);

		$controller->display($request, 'finance/social/find');
		return true;
	}
	
	function _orderby_x($controller, $row, $px) {
		$orderby = $this->orderby['1'];
		$row['orderby_x'] = $row['createtime'];
	
		if (empty($px) || $px == '-' || $px == '1') {
			$controller->assign('orderby', $orderby);
	
			return $row;
		}
	
		if ($px == '2') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['modifytime'];
		}
	
		if ($px == 'finish') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['finish'];
		}
	
		if ($px == 'emp') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $row['emp'];
		}
	
		if ($px == 'dept, id') {
			$orderby = $this->orderby[$px];
			$row['orderby_x'] = $this->_dept($row['dept']);
		}
	
		$controller->assign('orderby', $orderby);
	
		return $row;
	}
	
	function _where($request) {
		$name_s = $request['name_s'];
		$type_s = $request['type_s'];
	
		$date1_s = $request['date1_s'];
		$date2_s = $request['date2_s'];
	
		$my = $this->_my();
	
		if (empty($my)) {
			$where = " WHERE 1=1";
		} else {
			$where = " WHERE " . $my;
		}
	
		if (!empty($name_s)) {
			$where .= " AND name LIKE '%" . $name_s . "%'";
		}
		
		if (!empty($type_s) && $type_s <> '-') {
			$where .= " AND type = '" . $type_s . "'";
		}
	
		if (!empty($date1_s)) {
			$where .= " AND date >= '" . $date1_s . "'";
		}
	
		if (!empty($date2_s)) {
			$where .= " AND date < '" . $date2_s . "'";
		}
	
		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);
	
		return $where;
	}
	
	function _my() {
		if ($this->_is_admin()) {
			return '';
		}
	
		$emp = $_SESSION['id'];
	
		$p = array('1'=>'159', '2'=>'160', '3'=>'175');
	
		$a = $this->_audit_office($p);
		if (count($a) > 0) {
			$office = implode(",", $a);
				
			return sprintf(" visible = 1 AND (office in (%s) OR emp = '%s' OR oper = '%s')", $office, $emp, $emp);
		}
	
		$dept = $this->_where_dept($emp);
		if (!empty($dept)) {
			return sprintf(" visible = 1 AND (%s)", $dept);
		}
	
		return sprintf(" visible = 1 AND (emp = '%s' OR oper = '%s')", $emp, $emp);
	}
	
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();		
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$row = $this->_orderby_x($controller, $row, $this->orderby_s);

				$data[$count] = $row;
				$count++;
			}
			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'finance/social/list');
		return true;
	}
	
	function add($controller, $request) {
		$this->pretreat($controller);
	
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);
	
		$controller->display($request, 'finance/social/add');
		return true;
	}
	
	function addPost($controller, $request) {
		$this->pretreat($controller);
	
		$name = $request['name'];
		$type = $request['type'];
		$date = $request['date'];
		
		$salary = $this->_salary($id);
		
		$number = count($salary);
		$amount = $this->_amount_t2('file', $id);
		
		$content = addslashes($request['content']);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(office, dept, date, name, type, content, emp, oper, createtime, ";
		$fmt .= "modifytime) VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $date, $name, $type, $content, $emp, $_SESSION['id']);
		// debug($sql);
		$db->query($sql);
	
		$request['id_s'] = $db->insert_id();
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function ajax_add($controller, $request) {
		
		// debug(print_r($request, true));
		
		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			debug($file->getMessage());
		}
		
		$id = $this->_insert($request);
		
		$request['id'] = $id;
		
		// $row = $this->_view($id);
		
		$type = $request['type'];
	
		if ($file->isValid()) {
			$filename = $file->upload['tmp_name'];
			debug($filename);
	
			$this->_txt($id, $filename, $type);
			
			$this->_upload2($file, $request);
		} else {
			debug($file->getMessage());
		}
	
		echo "1";
		return true;
	}
	
	
	function edit($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
		$row = $this->_view($id);
	
		$controller->assign_by_ref('result', $row);
	
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);
	
		$controller->display($request, 'finance/social/edit');
		return true;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);
	
		// debug(print_r($request, true));
	
		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);
	
		$name = $request['name'];
		$type = $request['type'];
		$date = $request['date'];
		
		$salary = $this->_salary($id);
		
		$number = count($salary);
		$amount = $this->_amount_t2('file', $id);
		
		$content = addslashes($request['content']);
	
		$emp = $this->_input($request['emp']);
		$office = $this->_office($emp);
		$dept = $this->_emp_dept($emp);
	
		$db = newdb($this->dsn_w);
		$fmt = "UPDATE %s SET office = '%s', dept = '%s', emp = '%s', date = '%s', name = '%s', type = '%s', number = '%s', amount = '%s'";
		$fmt .= ", content = '%s', modifytime = now() WHERE id = '%s'";
		$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $date, $name, $type, $number, $amount, $content, $id);
		debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	/**
	 * 导入
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function import($controller, $request) {
		$this->pretreat($controller);
		
		$name = $this->_name_option();
		$controller->assign_by_ref('tt_option', $name);
	
		$controller->display($request, 'finance/social/import');
		return true;
	}
	
	function ajax_import($controller, $request) {
	
		// debug(print_r($request, true));
	
		require_once 'HTTP/Upload.php';
		$upload = new HTTP_Upload('en');
		$file = $upload->getFiles('Filedata');
		if (PEAR::isError($file)) {
			debug($file->getMessage());
		}
	
		$name = $request['name'];
	
		$type = $request['type'];
	
		if ($file->isValid()) {
			$filename = $file->upload['tmp_name'];
			debug($filename);
	
			$data = $this->_txt($filename, $type);
	
			$controller->assign('name', $name);
	
			$controller->assign_by_ref('result', json_encode($data));
	
			// $this->_upload2($file, $request);
		} else {
			debug($file->getMessage());
		}
	
		// echo "1";
	
		$controller->display($request, 'finance/social/preview');
		return true;
	}
	
	function _txt($file, $type) {
	
		$ret = array();
		$i = 1;
	
		$fd = fopen($file, "r");
		while (!feof($fd)) {
			$buf = $this->_to_utf8(fgets($fd));
			// echo $buf;
	
			switch($type) {
				case 1:
					// debug("269");
					$row = $this->_line_1($name, $oper, $buf);
					break;
				default:
					debug("default 308");
					break;
			}
	
			if ($row && is_array($row)) {
				$row['no'] = $i++;
				$row['ok'] = true;
				$ret[] = $row;
			}
		}
	
		fclose($fd);
	
		return $ret;
	}
	
	function _line_1($name, $oper, $buf) {
		if (strlen($buf) < 5 || preg_match("/^#/i", $buf)) {
			return false;
		}
	
		$a = preg_split("/(\t|;)/", $buf);
	
		$ret = array();
		
		$ret['name'] = $this->get_alpha($a, 'c');
		
		$ret['social'] = $this->get_alpha($a, 'b');
		$ret['idcard'] = $this->get_alpha($a, 'd');
		
		$ret['a'] = $this->get_alpha($a, 'h');
		$ret['a1'] = $this->get_alpha($a, 'i');
		$ret['a2'] = $this->get_alpha($a, 'j');
		
		$ret['b'] = $this->get_alpha($a, 'k');
		$ret['b1'] = $this->get_alpha($a, 'l');
		$ret['b2'] = $this->get_alpha($a, 'm');
		
		$ret['c'] = $this->get_alpha($a, 'n');
		$ret['c2'] = $this->get_alpha($a, 'o');
		
		$ret['d'] = $this->get_alpha($a, 'p');
		$ret['d1'] = $this->get_alpha($a, 'q');
		$ret['d2'] = $this->get_alpha($a, 'r');
		
		$ret['e'] = $this->get_alpha($a, 's');
		$ret['e2'] = $this->get_alpha($a, 't');
	
		if (empty($ret['social']) || !preg_match("/^(\d+)$/i", $ret['social'])) {
			return false;
		}
	
		// $ret['content'] = $this->get_alpha($a, 'e');
	
		// $emp_x = Common::id(Constant::tbl_emp, $ret['idcard']);
		$emp_x = Common::x(Constant::tbl_emp, 'idcard', $ret['idcard']);
		$ret['emp'] = $emp_x['id'];
		
		if ($emp_x['name'] <> $ret['name']) {
			$ret['emp'] = 0;
		}
		
		return $ret;
	}
	
	function get_alpha($a, $c) {
		$i = $this->alpha2num($c);
		$s = $a[$i];
	
		$s = trim($s);
		if (empty($s)) {
			return '';
		}
	
		$s = preg_replace("/[\"|\'|,]+/i", "", $s);
	
		return $s;
	}
	
	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function importPost($controller, $request) {
		$this->pretreat($controller);
	
		$this->_update($request['name'], $request['data']);
	
		$controller->assign('baseLink', $this->doGet($request, 'findPost'));
	
		$this->findPost($controller, $request);
		return true;
	}
	
	function _update($name, $data) {
		$data = json_decode($data, true);
		
		debug(print_r($data, true));
	
		$oper = $_SESSION['id'];
			
		$db = newdb($this->dsn_w);
		while (list($k, $v) = each($data)) {
	
			$idcard = $v['idcard'];
			$social = $v['social'];
			
			if (empty($idcard) || empty($social)) {
				continue;
			}
	
			if ($v['ok'] == '0') {
				continue;
			}
	
			$emp = $v['emp'];
			$office = $this->_office($emp);
			$dept = $this->_emp_dept($emp);
	
			$fmt = "SELECT * FROM %s WHERE AND social = '%s' AND name = '%s'";
			$sql = sprintf($fmt, $this->tbl, $social, $name);
			debug($sql);
			$result = $db->query($sql);
			if ($row = $db->fetch_array($result)) {
				$fmt = "UPDATE %s SET office = '%s', dept = '%s', social = '%s', a = '%s', a1 = '%s', a2 = '%s', b = '%s', b1 = '%s', b2 = '%s', c = '%s', c2 = '%s', d = '%s', d1 = '%s', d2 = '%s', e = '%s', e2 = '%s', content = '%s', modifytime = now() WHERE id = '%s'";
				$sql = sprintf($fmt, $this->tbl, $office, $dept, $v['social'], $v['a'], $v['a1'], $v['a2'], $v['b'], $v['b1'], $v['b2'], $v['c'], $v['c2'], $v['d'], $v['d1'], $v['d2'], $v['e'], $v['e2'], $row['id']);
			} else {
				$fmt = "INSERT INTO %s(office, dept, emp, name, social, a, a1, a2, b, b1, b2, c, c2, d, d1, d2, e, e2, content, oper, createtime, modifytime)";
				$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
				$sql = sprintf($fmt, $this->tbl, $office, $dept, $emp, $name, $v['social'], $v['a'], $v['a1'], $v['a2'], $v['b'], $v['b1'], $v['b2'], $v['c'], $v['c2'], $v['d'], $v['d1'], $v['d2'], $v['e'], $v['e2'], $v['content'], $oper);
			}
	
			debug($sql);
			$db->query($sql);
		}
	}
	
	function upload($controller, $request) {
		$this->pretreat($controller);
	
		$id = $request['id'];
	
		$this->_files_clean($id);
	
		$row = $this->_view($id);
		
		$row['files'] = $this->_files($row['id']);
	
		$controller->assign_by_ref('result', $row);
	
		$controller->display($request, 'finance/salary/file/upload');
		return true;
	}
	
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];

		clean($this->tbl . "_id_" . $id);
		$row = $this->_view($id);
		
		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'finance/social/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['type_t'] = $this->type[$row['type']];
 	
		$row = $this->_view_t($row);
	
		// debug(print_r($row, true));
	
		return $row;
	}
	
	function _list() {
		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE status IN (1, 2) ORDER BY id DESC";
		$sql = sprintf($fmt, $this->tbl);
		debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row = $this->_view(null, $row);
				
			// $ret[$row['id']] = $row['id'] . "/" . $row['customer_t'] . "/" . $row['amount'];
			$ret[] = $row;
		}
	
		return $ret;
	}
	
	function t($controller, $request) {
		$db = newdb($this->dsn_w);
	
		$dd = date("Y-m-d", strtotime("-3 day"));
	
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		echo $sql . "\n";
		$result = $db->query($sql);
	
		while ($row = $db->fetch_array($result)) {
			print_r($row);
				
			$salary = $this->_salary($row['id']);
		
			$number = count($salary);
			$amount = $this->_amount_t2('file', $row['id']);
				
			$fmt = "UPDATE %s SET number = '%s', amount = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $number, $amount, $row['id']);
			echo $sql . "\n";
			$db->query($sql);
		}
	
		echo "\n" . $_REQUEST['do'] . "\n";
	
		logdate($controller, $request);
		return true;
	}
}
?>